﻿Partial Public Class GridPlegable
    Inherits ContentControl

    Public Shared ReadOnly TitleProperty As DependencyProperty = _
                                                DependencyProperty.Register("Title", _
                                                GetType(String), _
                                                GetType(GridPlegable), _
                                                Nothing)
    Public Shared ReadOnly EstatProperty As DependencyProperty = _
                                            DependencyProperty.Register("Estat", _
                                            GetType(EnumEstatPlegat), _
                                            GetType(GridPlegable), _
                                            Nothing)

    Public Property Title() As String
        Get
            Return CStr(GetValue(TitleProperty))
        End Get
        Set(ByVal value As String)
            SetValue(TitleProperty, value)
        End Set
    End Property

    Public Property Estat() As EnumEstatPlegat
        Get
            Return GetValue(EstatProperty)
        End Get
        Set(ByVal value As EnumEstatPlegat)
            SetValue(EstatProperty, value)
            If _GridPrincipal IsNot Nothing Then
                If value = EnumEstatPlegat.Desplegat Then
                    Me._GridPrincipal.Visibility = Windows.Visibility.Visible
                    Me._BotoPlegar.Source = New Media.Imaging.BitmapImage(New Uri("/LullabyFullDeCaixa;component/Imatges/desplegar.png", UriKind.Relative))
                ElseIf value = EnumEstatPlegat.Plegat Then
                    Me._GridPrincipal.Visibility = Windows.Visibility.Collapsed
                    Me._BotoPlegar.Source = New Media.Imaging.BitmapImage(New Uri("/LullabyFullDeCaixa;component/Imatges/plegar.png", UriKind.Relative))
                End If
            End If
        End Set
    End Property

    Public Sub New()
        MyBase.New()
        Me.DefaultStyleKey = GetType(GridPlegable)
    End Sub

    Public Enum EnumEstatPlegat
        Plegat
        Desplegat
    End Enum

    Private Sub Plegar_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles _BotoPlegar.MouseLeftButtonUp
        If Estat = EnumEstatPlegat.Desplegat Then
            Estat = EnumEstatPlegat.Plegat
        ElseIf Estat = EnumEstatPlegat.Plegat Then
            Estat = EnumEstatPlegat.Desplegat
        End If
    End Sub

    Private _GridPrincipal As Grid
    Private WithEvents _BotoPlegar As Image

    Public Overloads Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()

        'Busquem l'objecte Grid Principal
        _GridPrincipal = DirectCast(GetTemplateChild("GridPrincipal"), Grid)
        _BotoPlegar = DirectCast(GetTemplateChild("BotoPlegar"), Image)

        'Refresquem la visibilitat de la grid
        Estat = Estat
    End Sub

End Class
